/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Clock.java
 *
 * Created on 25.01.2010, 04:32:52
 */
package nixieclock;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.WindowConstants;

import swinglayout.GroupLayout;
import swinglayout.LayoutStyle;

/**
 * 
 * @author hansolo
 */
public class Clock extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Timer timer = new Timer(500, this);

	/** Creates new form Clock */
	public Clock() {
		initComponents();
		timer.start();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated
	// Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		backgroundPanel = new BackgroundPanel();
//		backgroundPanel = new JPanel();
		hourLeft = new NixieNumber();
		hourRight = new NixieNumber();
		minLeft = new NixieNumber();
		minRight = new NixieNumber();
		secLeft = new NixieNumber();
		secRight = new NixieNumber();

		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		backgroundPanel.setName("backgroundPanel"); // NOI18N

		hourLeft.setName("hourLeft"); // NOI18N

		GroupLayout hourLeftLayout = new GroupLayout(hourLeft);
		hourLeft.setLayout(hourLeftLayout);
		hourLeftLayout.setHorizontalGroup(hourLeftLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 86, Short.MAX_VALUE));
		hourLeftLayout.setVerticalGroup(hourLeftLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 146, Short.MAX_VALUE));

		hourRight.setName("hourRight"); // NOI18N

		GroupLayout hourRightLayout = new GroupLayout(hourRight);
		hourRight.setLayout(hourRightLayout);
		hourRightLayout.setHorizontalGroup(hourRightLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 86, Short.MAX_VALUE));
		hourRightLayout.setVerticalGroup(hourRightLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 146, Short.MAX_VALUE));

		minLeft.setName("minLeft"); // NOI18N

		GroupLayout minLeftLayout = new GroupLayout(minLeft);
		minLeft.setLayout(minLeftLayout);
		minLeftLayout.setHorizontalGroup(minLeftLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 86, Short.MAX_VALUE));
		minLeftLayout.setVerticalGroup(minLeftLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 146, Short.MAX_VALUE));

		minRight.setName("minRight"); // NOI18N

		GroupLayout minRightLayout = new GroupLayout(minRight);
		minRight.setLayout(minRightLayout);
		minRightLayout.setHorizontalGroup(minRightLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 86, Short.MAX_VALUE));
		minRightLayout.setVerticalGroup(minRightLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 146, Short.MAX_VALUE));

		secLeft.setName("secLeft"); // NOI18N

		GroupLayout secLeftLayout = new GroupLayout(secLeft);
		secLeft.setLayout(secLeftLayout);
		secLeftLayout.setHorizontalGroup(secLeftLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 86, Short.MAX_VALUE));
		secLeftLayout.setVerticalGroup(secLeftLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 146, Short.MAX_VALUE));

		secRight.setName("secRight"); // NOI18N

		GroupLayout secRightLayout = new GroupLayout(secRight);
		secRight.setLayout(secRightLayout);
		secRightLayout.setHorizontalGroup(secRightLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 86, Short.MAX_VALUE));
		secRightLayout.setVerticalGroup(secRightLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 146, Short.MAX_VALUE));

		GroupLayout backgroundPanelLayout = new GroupLayout(backgroundPanel);
		backgroundPanel.setLayout(backgroundPanelLayout);
		backgroundPanelLayout.setHorizontalGroup(backgroundPanelLayout
				.createParallelGroup(GroupLayout.LEADING).add(
						backgroundPanelLayout.createSequentialGroup()
								.addContainerGap().add(hourLeft,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(LayoutStyle.UNRELATED).add(
										hourRight, GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE).add(78, 78,
										78).add(minLeft,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(LayoutStyle.UNRELATED).add(
										minRight, GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE).add(73, 73,
										73).add(secLeft,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(LayoutStyle.UNRELATED).add(
										secRight, GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addContainerGap(GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
		backgroundPanelLayout
				.setVerticalGroup(backgroundPanelLayout
						.createParallelGroup(GroupLayout.LEADING)
						.add(
								backgroundPanelLayout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												backgroundPanelLayout
														.createParallelGroup(
																GroupLayout.LEADING)
														.add(
																secLeft,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.add(
																minRight,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.add(
																minLeft,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.add(
																hourRight,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.add(
																hourLeft,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.add(
																secRight,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				GroupLayout.LEADING).add(backgroundPanel,
				GroupLayout.PREFERRED_SIZE, 749, GroupLayout.PREFERRED_SIZE));
		layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.LEADING)
				.add(backgroundPanel, GroupLayout.PREFERRED_SIZE, 186,
						GroupLayout.PREFERRED_SIZE));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	@Override
	public void actionPerformed(ActionEvent event) {
		// Hours
		if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) < 10) {
			hourLeft.setNumber(0);
			hourRight.setNumber(Calendar.getInstance()
					.get(Calendar.HOUR_OF_DAY));
		} else {
			hourLeft.setNumber(Integer.parseInt(Integer.toString(
					Calendar.getInstance().get(Calendar.HOUR_OF_DAY))
					.substring(0, 1)));
			hourRight.setNumber(Integer.parseInt(Integer.toString(
					Calendar.getInstance().get(Calendar.HOUR_OF_DAY))
					.substring(1, 2)));
		}

		// Minutes
		if (Calendar.getInstance().get(Calendar.MINUTE) < 10) {
			minLeft.setNumber(0);
			minRight.setNumber(Calendar.getInstance().get(Calendar.MINUTE));
		} else {
			minLeft.setNumber(Integer.parseInt(Integer.toString(
					Calendar.getInstance().get(Calendar.MINUTE))
					.substring(0, 1)));
			minRight.setNumber(Integer.parseInt(Integer.toString(
					Calendar.getInstance().get(Calendar.MINUTE))
					.substring(1, 2)));
		}

		// Seconds
		if (Calendar.getInstance().get(Calendar.SECOND) < 10) {
			secLeft.setNumber(0);
			secRight.setNumber(Calendar.getInstance().get(Calendar.SECOND));
		} else {
			secLeft.setNumber(Integer.parseInt(Integer.toString(
					Calendar.getInstance().get(Calendar.SECOND))
					.substring(0, 1)));
			secRight.setNumber(Integer.parseInt(Integer.toString(
					Calendar.getInstance().get(Calendar.SECOND))
					.substring(1, 2)));
		}
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				Clock app = new Clock();
				app.setLocationRelativeTo(null);
				app.setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
//	private BackgroundPanel backgroundPanel;
	private JPanel backgroundPanel;
	private NixieNumber hourLeft;
	private NixieNumber hourRight;
	private NixieNumber minLeft;
	private NixieNumber minRight;
	private NixieNumber secLeft;
	private NixieNumber secRight;
	// End of variables declaration//GEN-END:variables
}
